<?php
include './meihe/common.php';
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
header('Content-Type: text/html; charset=utf-8');

$tqq_url = '';

if (isset($_GET['longurl']) || isset($_POST['longurl'])) {
    $value = isset($_GET['longurl']) ? $_GET['longurl'] : $_POST['longurl'];
    $value = preg_match('/^http[s]?:\/\//', $value) ? $value : 'http://' . $value;
    $value = preg_match('/[-�]/', $value) ? exit('{"code":-1,"msg":"请您输入正确的域名！"}') : $value;
    $longurl = daddslashes($value);

    $row = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . $domain . '\' limit 1');
    if (empty($row)) {
        $row = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . get_host($domain) . '\' and `fhy`=\'2\' limit 1');
    }

    $count = $DB->count("SELECT count(*) FROM meihe_report WHERE url='{$value}'");
    if ($row['type'] != 0x001) {
        if ($count > $conf['sccs']) {
            exit('{"msg":"免费生成次数用完，已禁止生成！"}');
        }
    }

    $api = isset($_GET['api']) ? $_GET['api'] : (isset($_POST['api']) ? $_POST['api'] : null);

    if ($api == 0x001) {
        $keyrow = $DB->get_row("SELECT * FROM meihe_user WHERE api_key='{$key}' AND limitt=1");
        if ($keyrow && $keyrow['active'] == 0x001) {
            $_SESSION['user_login_mail'] = TIMESTAMP;
            if ($format === 'txt') {
                $result = array('code' => 0x001, 'ae_url' => $tqq_url, 'haohao_api' => $tqq_url, 'haohao_url' => $longurl);
                exit(json_encode($result));
            }
            exit($tqq_url);
        }
        exit('{"code":-1,"msg":"API对接密钥无法使用！请检查账号是否被封"}');
    }

    if ($api == 0x0002) {
        exit('{"msg":"防洪方式不存在！"}');
    }

    if ($api == 0x00003) {
        $dljk = $apihost['dljk'] == 0x05 ? $apihost['apiurl'] : $apihost['duanl'] . 'ajax.php?act=dwzapi&url=';
        $tqq_url = file_get_contents($dljk . urlencode($resulturl));
        $tqq_url = json_decode($tqq_url, true);
        $tqq_url = $tqq_url[$apihost['fhzd']];
        if (!$tqq_url) {
            exit('{"code":-1,"msg":"生成失败请更换短链接口！"}');
        }
        if ($format === 'txt') {
            $result = array('code' => 0x001, 'ae_url' => $tqq_url, 'haohao_api' => $tqq_url, 'haohao_url' => $longurl);
            exit(json_encode($result));
        }
        exit($tqq_url);
    }

    if ($api == 0x001) {
        $keyrow = $DB->get_row("SELECT * FROM meihe_user WHERE api_key='{$key}' AND limitt=1");
        if ($keyrow && $keyrow['active'] == 0x001) {
            $_SESSION['user_login_mail'] = TIMESTAMP;
            $result = array('code' => 0x001, 'ae_url' => $tqq_url, 'haohao_api' => $tqq_url, 'haohao_url' => $longurl);
            exit(json_encode($result));
        }
        exit('{"code":-1,"msg":"API对接密钥无法使用！请检查账号是否被封"}');
    }

    if ($count > $conf['sccs']) {
        exit('{"msg":"免费生成次数用完，已禁止生成！"}');
    }

    if (isset($_GET['type']) || isset($_POST['type'])) {
        $vvalue = isset($_GET['type']) ? $_GET['type'] : $_POST['type'];
        if ($vvalue == null) {
            $vvalue = $conf['duan'];
        }
        if ($vvalue == 0x0003) {
            $dljk = $apihost['apiurl'];
        } elseif ($vvalue == 0x000004) {
            $dljk = $apihost['dljk'] == 0x05 ? $apihost['apiurl'] : $apihost['duanl'] . 'ajax.php?act=dwzapi&url=';
        } else {
            exit('{"code":-1,"msg":"API对接密钥错误！"}');
        }

        $tqq_url = file_get_contents($dljk . urlencode($resulturl));
        $tqq_url = json_decode($tqq_url, true);
        $tqq_url = $tqq_url[$apihost['fhzd']];
        if (!$tqq_url) {
            exit('{"code":-1,"msg":"生成失败请更换短链接口！"}');
        }
        if ($format === 'txt') {
            $result = array('code' => 0x001, 'ae_url' => $tqq_url, 'haohao_api' => $tqq_url, 'haohao_url' => $longurl);
            exit(json_encode($result));
        }
        exit($tqq_url);
    }

    if (!$apihost || $apihost['jktz'] != 0x001) {
        exit('{"msg":"短链类型不存在！"}');
    }

    $current_time = time();
    if ($conf['hyjmsc'] != 0 && isset($_SESSION['last_submit_time'])) {
        $time_difference = $current_time - $_SESSION['last_submit_time'];
        $remaining_seconds = $conf['hyjmsc'] - $time_difference;
        if ($time_difference < $conf['hyjmsc']) {
            exit('{"code":-1,"msg":"生成频率太高！请等待' . $remaining_seconds . '秒后再生成"}');
        }
    }

    $_SESSION['last_submit_time'] = $current_time;
}

exit('{"msg":"当前短链接口会员才能使用！"}');
